<?php

namespace App\Http\Controllers\admin;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class Banner extends Controller
{
    //轮播图列表
    public function index(Request $request)
    {
        //实现分页
        $data['page'] = (int)$request->page;//获取分页
        $data['pageSize'] = 10;
        $pages = DB::table('banner')->orderBy('bid','desc')->pages($data['pageSize']);
        $data['banner_list'] = $pages['lists'];
        $data['total']        = $pages['total']; //总数
        return view('admin.banner.index',$data);
    }
    public function add(Request $request)
    {
        $bid = (int)$request->bid;
        $data['banner']  = DB::table('banner')->where('bid',$bid)->item();
        return view('admin.banner.add',$data);
    }
    public function save(Request $request)
    {
        $bid              = (int)$request->bid;
        $data['title']    = trim($request->title);
        $data['url'] = trim($request->url);
        $data['thumb']    = trim($request->thumb);
        $data['desc']    = trim($request->desc);
        $data['status']   = (int)$request->status;
        if ($data['title'] == ""){
            exit(json_encode(array('code'=>1,'msg'=>'请输入轮播图标题')));
        }
        if ($bid == 0){
            $data['add_time'] = time();
            DB::table('banner')->insert($data);
        }else{
            $data['update_time'] = time();
            DB::table('banner')->where('bid',$bid)->update($data);
        }
        exit(json_encode(array('code'=>0,'msg'=>'保存成功')));
    }
    public function del(Request $request)
    {
        $bid = (int)$request->bid;
        DB::table('banner')->where('bid',$bid)->delete();
        exit(json_encode(array('code'=>0,'msg'=>'删除成功')));
    }
}
